<template>
  <view class="settings-container">
    <view class="settings-header">
      <view class="header-content">
        <text class="back-icon" @click="goBack">←</text>
        <text class="header-title">设置</text>
      </view>
    </view>
    
    <!-- 轮播广告 -->
    <view class="banner-wrapper">
      <ad-banner></ad-banner>
    </view>
    
    <view class="settings-section">
      <text class="section-title">界面设置</text>
      
      <view class="setting-item">
        <text class="setting-label">背景颜色</text>
        <view class="color-picker">
          <view 
            v-for="(color, index) in colorOptions" 
            :key="index"
            class="color-option"
            :style="{ backgroundColor: color.value }"
            :class="{ active: currentColor === color.value }"
            @click="selectColor(color.value)"
          ></view>
        </view>
      </view>
      
      <view class="setting-item">
        <text class="setting-label">自定义颜色</text>
        <view class="custom-color">
          <input 
            type="text" 
            v-model="customColor" 
            placeholder="#RRGGBB" 
            class="color-input"
          />
          <button class="apply-btn" @click="applyCustomColor">应用</button>
        </view>
      </view>
    </view>
    
    <view class="settings-section">
      <text class="section-title">预览</text>
      <view class="preview-container" :style="{ backgroundColor: currentColor }">
        <text class="preview-text">预览效果</text>
      </view>
    </view>
    
    <view class="settings-footer">
      <button class="save-btn" @click="saveSettings">保存设置</button>
      <button class="reset-btn" @click="resetSettings">恢复默认</button>
    </view>
  </view>
</template>

<script>
import AdBanner from '@/components/AdBanner.vue'

export default {
  components: {
    AdBanner
  },
  data() {
    return {
      currentColor: '#1e5799',
      customColor: '',
      colorOptions: [
        { name: '默认蓝', value: '#1e5799' },
        { name: '深绿', value: '#2e8b57' },
        { name: '紫色', value: '#4b0082' },
        { name: '深红', value: '#8b0000' },
        { name: '橙色', value: '#ff8c00' },
        { name: '青色', value: '#008b8b' }
      ]
    }
  },
  onLoad() {
    // 从本地存储加载保存的颜色设置
    const savedColor = uni.getStorageSync('appBackgroundColor')
    if (savedColor) {
      this.currentColor = savedColor
    }
  },
  methods: {
    goBack() {
      uni.navigateBack()
    },
    selectColor(color) {
      this.currentColor = color
    },
    applyCustomColor() {
      // 验证颜色格式
      const colorRegex = /^#[0-9A-Fa-f]{6}$/
      if (colorRegex.test(this.customColor)) {
        this.currentColor = this.customColor
      } else {
        uni.showToast({
          title: '请输入有效的颜色代码',
          icon: 'none'
        })
      }
    },
    saveSettings() {
      // 保存颜色设置到本地存储
      uni.setStorageSync('appBackgroundColor', this.currentColor)
      
      // 通知全局状态更新
      uni.$emit('updateBackgroundColor', this.currentColor)
      
      uni.showToast({
        title: '设置已保存',
        icon: 'success'
      })
      
      // 返回上一页
      setTimeout(() => {
        uni.navigateBack()
      }, 1500)
    },
    resetSettings() {
      this.currentColor = '#1e5799'
      this.customColor = ''
      
      uni.showToast({
        title: '已恢复默认设置',
        icon: 'none'
      })
    }
  }
}
</script>

<style>
.settings-container {
  padding: 20px;
  min-height: 100vh;
  background-color: #f5f7fa;
}

.settings-header {
  margin-bottom: 30px;
}

.header-content {
  display: flex;
  align-items: center;
  gap: 10px;
}

.back-icon {
  font-size: 24px;
  cursor: pointer;
}

.header-title {
  font-size: 24px;
  font-weight: bold;
  color: #333;
}

.settings-section {
  background-color: #fff;
  border-radius: 10px;
  padding: 20px;
  margin-bottom: 20px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.05);
}

.section-title {
  font-size: 18px;
  font-weight: bold;
  color: #333;
  margin-bottom: 15px;
  display: block;
}

.setting-item {
  margin-bottom: 20px;
}

.setting-label {
  font-size: 16px;
  color: #666;
  margin-bottom: 10px;
  display: block;
}

.color-picker {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
}

.color-option {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  border: 2px solid #ddd;
  cursor: pointer;
  transition: all 0.3s;
}

.color-option.active {
  border-color: #333;
  transform: scale(1.1);
}

.custom-color {
  display: flex;
  gap: 10px;
}

.color-input {
  flex: 1;
  height: 40px;
  border: 1px solid #ddd;
  border-radius: 5px;
  padding: 0 10px;
  font-size: 14px;
}

.apply-btn {
  width: 80px;
  height: 40px;
  background-color: #1e5799;
  color: white;
  border: none;
  border-radius: 5px;
  font-size: 14px;
}

.preview-container {
  height: 100px;
  border-radius: 10px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.preview-text {
  color: white;
  font-size: 16px;
  font-weight: bold;
}

.settings-footer {
  display: flex;
  gap: 15px;
  margin-top: 30px;
}

.save-btn, .reset-btn {
  flex: 1;
  height: 45px;
  border-radius: 5px;
  font-size: 16px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.save-btn {
  background-color: #1e5799;
  color: white;
  border: none;
}

.reset-btn {
  background-color: #f5f5f5;
  color: #666;
  border: 1px solid #ddd;
}

/* 轮播广告容器 */
.banner-wrapper {
  width: 100%;
  padding: 0 15px;
  margin: 10px 0;
  box-sizing: border-box;
}
</style> 